package com.gc.controller;

import com.gc.common.constants.FileConstants;
import com.gc.common.constants.SysConstants;
import com.gc.common.resp.ApiResult;
import com.gc.common.util.MyUtil;
import com.gc.dto.VehicleResultImageDTO;
import com.gc.pojo.SysMhyMedia;
import com.gc.pojo.SysMhyProcessDataInfo;
import com.gc.pojo.SysMhyProcessNoInfo;
import com.gc.pojo.SysMhySmokeDetectionInfo;
import com.gc.services.MhyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("mhy")
public class MhyController {
    @Autowired
    private MhyService mhyService;

    /**
     * 上传 获得检测流水号实体类
     *
     * @param processNoInfo
     * @return
     */
    @PostMapping("/et/process")
    public ApiResult process(@RequestBody SysMhyProcessNoInfo processNoInfo) {
        int count = mhyService.addProcessNoInfo(processNoInfo);
        if (count == 1) {
            return new ApiResult();
        } else {
            return new ApiResult(500, "添加失败!");
        }
    }

    /**
     * 上传检测过程数据
     *
     * @param processDataInfos
     * @return
     */
    @PostMapping("/et/process/data/batch")
    public ApiResult batch(@RequestBody List<SysMhyProcessDataInfo> processDataInfos) {
        int count = mhyService.addProcessDataInfoList(processDataInfos);
        if (count >= 1) {
            return new ApiResult();
        } else {
            return new ApiResult(500, "添加失败!");
        }
    }

    /**
     * 上传检测结果实体类
     *
     * @param smokeDetectionInfo
     * @return
     */
    @PostMapping("/et/result")
    public ApiResult result(@RequestBody SysMhySmokeDetectionInfo smokeDetectionInfo) {
        int count = 0;
        SysMhySmokeDetectionInfo temp = mhyService.selectMhySmokeDetectionInfoByUUId(smokeDetectionInfo.getUuid());
        if (ObjectUtils.isEmpty(temp)) {
            count = mhyService.addMhySmokeDetectionInfo(smokeDetectionInfo);
        } else {
            smokeDetectionInfo.setSmokeDetectionId(temp.getSmokeDetectionId());
            count = mhyService.modifyMhySmokeDetectionInfoById(smokeDetectionInfo);
        }
        if (count == 1) {
            return new ApiResult();
        } else {
            return new ApiResult(500, "添加失败!");
        }
    }


    /**
     * 上传检测结果照片
     *
     * @param vri
     * @return
     */
//    @PostMapping("/et/image/upload")
//    @ResponseBody
//    public ApiResult imageResultUpload(@RequestBody @Validated VehicleResultImageDTO vri) {
//        int count;
//        String uuid = null;
//        SysMhyMedia mm = mhyService.getMhyMediaByProcessNo(vri.getProcessNo());
//        vri.setFileData(MyUtil.Base64ToImage(vri.getFileData(), FileConstants.VOR_IMG_LOCAL_URL));
//        if (ObjectUtils.isEmpty(mm)) {
//            SmokeDetection sd = mhyService.getSmokeDetectionInfoByProcessNo(vri.getProcessNo());
//            if (ObjectUtils.isEmpty(sd)) {
//                count = mhyService.addMhyMedia(vri);
//            } else {
//                uuid = sd.getUuid();
//                MhyMedia media = mhyService.getMhyMediaByUUId(uuid);
//                if (ObjectUtils.isEmpty(media)) {
//                    count = mhyService.addMhyMedia(vri);
//                } else {
//                    count = mhyService.modifyMhyMedia(vri, media.getMediaId());
//                }
//            }
//        } else {
//            count = mhyService.modifyMhyMedia(vri, vri.getProcessNo());
//        }
//        if (count == 0) {
//            return new ApiResult(500, "上传错误, 请对照字段");
//        } else {
//            if (!StringUtils.isEmpty(uuid)) {
//                VeiReportInfo info = veiReportInfoService.getReportByUUId(uuid);
//                if (!ObjectUtils.isEmpty(info)) {
//                    VehicleImage vehicleImage = new VehicleImage();
//                    vehicleImage.setFileData(vri.getFileData());
//                    vehicleImage.setFilename(vri.getFilename());
//                    vehicleImage.setUuid(uuid);
//                    int count2 = veiReportInfoService.modifyAndroidReportImage(info, vehicleImage);
//                }
//            }
//        }
//        return new ApiResult();
//    }

    /**
     * 上传检测结果视频
     *
     * @param file
     * @return
     */
    @PostMapping("/et/video/upload")
    public ApiResult videoUpload(@RequestParam String processNo, MultipartFile file) {
        SysMhyMedia mm = mhyService.getMhyMediaByProcessNo(processNo);
        if (ObjectUtils.isEmpty(mm)) {
            return new ApiResult(500, "此流水号无效!");
        }
        InputStream inputStream;
        OutputStream os;
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        try {
            byte[] bs = new byte[1024];
            int len;
            inputStream = file.getInputStream();
            File tempFile = new File(FileConstants.VIDEO_LOCAL_URL);
            if (!tempFile.exists()) {
                tempFile.mkdirs();
            }
            os = new FileOutputStream(tempFile.getPath() + File.separator + uuid + ".mp4");
            while ((len = inputStream.read(bs)) != -1) {
                os.write(bs, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        SysMhyMedia mMM = new SysMhyMedia();
        mMM.setMediaId(mm.getMediaId());
        mMM.setVideo(uuid + ".mp4");
        int count = mhyService.modifyMhyMediaById(mMM);
        if (count == 1) {
            return new ApiResult();
        } else {
            return new ApiResult(500, "添加失败!");
        }
    }
}
